package com.renli.controller;

import com.renli.bean.ResultJson;
import com.renli.pojo.Role;
import com.renli.pojo.Train;
import com.renli.pojo.TrainFeedback;
import com.renli.service.Train_planService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.mapstruct.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2020/09/02 15:01
 * Description:
 * Version: V1.0
 */
@Controller
@Api(value = "培训管理", tags = "培训管理接口")
public class Train_PlanController {
    @Autowired
    private Train_planService train_planService;

    @RequestMapping(value = "/Train_Plan/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加培训计划接口", notes = "对培训计划进行添加", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson add_plan(@RequestBody Train train) {
        ResultJson resultJson = new ResultJson();
        System.out.println("--Train_PlanController--");
        Train train1 = new Train();
        System.out.println(train.getName());
        int re = train_planService.add_plan(train);
        if (re > 0) {
            resultJson.getCode();
            return new ResultJson().setSuccess("更改成功");
        } else {
            return new ResultJson().setFail("更改失败");
        }
    }

    @RequestMapping(value = "/Train_Plan/query")
    @ApiOperation(value = "根据条件查询培训计划", notes = "查询培训计划", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public List<Train> query(@RequestBody @ApiParam(name = "map",value = "name,means,starTtime") Map<String,String> map) {

        String name = map.get("name");
        String means = map.get("means");
        String starTtime = map.get("starTtime");
        System.out.println(name);
        System.out.println(means);
        System.out.println(starTtime);
        List<Train> query = train_planService.query(name, means, starTtime);


        return query;
    }


    @RequestMapping(value = "/Train_Plan/name", method = RequestMethod.POST)
    @ApiOperation(value = "查询讲师名", notes = "查询出讲师名字", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson queryName(String name) {

        List<String> list = train_planService.role_name(name);

        ResultJson resultJson = new ResultJson();
        resultJson.setSuccess("成功", list);
        System.out.println("--Train_PlanControllerquer--");

        if (list.size() > 0) {

            return resultJson;
        } else
            return resultJson.setFail("查询失败");
    }

    @RequestMapping(value = "/Train_Plan/return")
    @ApiOperation(value = "查询复核信息", notes = "根据name查询复核信息", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public Train query_return(String name) {
        System.out.println(name);
        System.out.println("Controller");
        return train_planService.query_reunite(name);
    }

    @RequestMapping(value = "/Train_Plan/return/start")
    @ApiOperation(value = "更改复核属性", notes = "将该文件改为复核成功", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson update_reunite(String name){
        int row = train_planService.update_reunite(name);
        if (row>0){
            return new ResultJson().setSuccess("更改成功");
        }else {
            return  new ResultJson().setFail("更改失败");
        }
    }


    @RequestMapping(value = "/Train_Plan/Feedback", method = RequestMethod.POST)
    @ApiOperation(value = "插入反馈数据", notes = "添加反馈", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson insert_fk(@RequestBody TrainFeedback trainFeedback) {
//        System.out.println(name);
        int row = train_planService.insertFk(trainFeedback);
        System.out.println(trainFeedback.getGrade());
        if (row > 0) {
            return new ResultJson().setSuccess("插入成功");
        } else
            return new ResultJson().setFail("插入失败");
    }


    @RequestMapping(value = "/Train_Plan/Feedback/query")
    @ApiOperation(value = "查询反馈数据", notes = "查询反馈", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public Train query_fk(String name) {
        System.out.println("/Train_Plan/Feedback/query");
        Train train = train_planService.query_feedback(name);
        return train;
    }

    /**
     * 修改状态 假删除
     *
     * @param name
     * @return
     */
    @RequestMapping("/Train_Plan/del")
    @ApiOperation(value = "假删除表数据", notes = "根据name假删除属性", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson del_trin(@RequestParam("name") String name) {
        ResultJson resultJson = new ResultJson();
        System.out.println(name);
        int re = train_planService.del_trin(name);
        System.out.println("=====dellll====");
        if (re > 0) {
            return resultJson.setSuccess("更改成功");
        } else
            return resultJson.setFail("更改失败");
    }


    @RequestMapping(value = "/Train_Plan/update",method = RequestMethod.POST)
    @ApiOperation(value = "更改培训计划属性", notes = "根据name修改属性", httpMethod = "post", response = ResultJson.class)
    @ResponseBody
    public ResultJson update_trin(@RequestBody Train train) {
        System.out.println("/Train_Plan/update");
        int row = train_planService.update_trin(train);
        if (row > 0) {
            return new ResultJson().setSuccess("成功");
        } else
            return new ResultJson().setFail("失败");
    }

    @RequestMapping("/uploadFile")
    public ResultJson uploadFile(@RequestParam("headImg") CommonsMultipartFile headImg) throws IOException {//request  part
        //CommonsMultipartFile headImg  临时文件
        String fileName = headImg.getOriginalFilename();//获取文件名
        //获取后缀
        String newFileName = UUID.randomUUID().toString().replace("-", "")
                + fileName.substring(fileName.lastIndexOf("."));//工具类执行
        System.out.println("文件的新名字" + newFileName);
        //实例化一个文件对象
        File newFile = new File("D:\\xzzy\\class2\\src\\main\\target\\renli\\downlode", newFileName);
        //CommonsMultipartFile  复制
        headImg.transferTo(newFile);//  把临时文件中 内容复制给newFile
        ResultJson resultJson = new ResultJson();

        return resultJson.setSuccess("传输成功");
    }
}
